﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace NaviRankChecker
{
    public partial class rankHistory : Form
    {

        //Constants
        const int AW_SLIDE = 0X40000;
        const int AW_HOR_POSITIVE = 0X1;
        const int AW_HOR_NEGATIVE = 0X2;
        const int AW_BLEND = 0X80000;
        private Boolean drag;
        private int mousex;
        private int mousey;
        private static String XML_PATH = AppDomain.CurrentDomain.BaseDirectory + "/SearchHistory.xml";
        [Flags]
        enum AnimateWindowFlags
        {
            AW_HOR_POSITIVE = 0x00000001,
            AW_HOR_NEGATIVE = 0x00000002,
            AW_VER_POSITIVE = 0x00000004,
            AW_VER_NEGATIVE = 0x00000008,
            AW_CENTER = 0x00000010,
            AW_HIDE = 0x00010000,
            AW_ACTIVATE = 0x00020000,
            AW_SLIDE = 0x00040000,
            AW_BLEND = 0x00080000
        }

        [DllImport("user32")]
        static extern bool AnimateWindow(IntPtr hwnd, int time, AnimateWindowFlags flags);

        private NaviRankChecker.appMain _appMain;
        public rankHistory()
        {       
            InitializeComponent();
            
        }
       
        public rankHistory(NaviRankChecker.appMain appMain)
        {
            // TODO: Complete member initialization
            InitializeComponent();
            _appMain = appMain;
        }
     

        private void cmdCLose_Click(object sender, EventArgs e)
        {
            Globals.FormActivated = false;
            AnimateWindow(this.Handle, 250, AnimateWindowFlags.AW_HIDE | AnimateWindowFlags.AW_HOR_NEGATIVE);
            this.Dispose();
        }

        private void rankHistory_FormClosing(object sender, FormClosingEventArgs e)
        {
            AnimateWindow(this.Handle, 500, AnimateWindowFlags.AW_BLEND | AnimateWindowFlags.AW_HOR_NEGATIVE);
        }

        private void topPan_MouseDown(object sender, MouseEventArgs e)
        {
            this.drag = true;
            mousex = System.Windows.Forms.Cursor.Position.X - this.Left;
            mousey = System.Windows.Forms.Cursor.Position.Y - this.Top;
        }

        private void topPan_MouseMove(object sender, MouseEventArgs e)
        {
            if (drag)
            {
                this.Top = System.Windows.Forms.Cursor.Position.Y - mousey;
                this.Left = System.Windows.Forms.Cursor.Position.X - mousex;
            }
        }

        private void topPan_MouseUp(object sender, MouseEventArgs e)
        {
            this.drag = false;
        }

        public delegate void passNewData(string newKey, string newUrl);
        public passNewData pnd;
        private void dgHistory_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridViewCell xCell = dgHistory.CurrentCell;
            if (xCell!=null)
            {
                if (pnd!=null)
                {
                    pnd(dgHistory.CurrentRow.Cells["Keyword"].Value.ToString(), dgHistory.CurrentRow.Cells["URL"].Value.ToString());
                }
             
            }
        }

        private void rankHistory_Load(object sender, EventArgs e)
        {
           // bgParentListener.RunWorkerAsync();
            int WidthOfMain = Application.OpenForms["appMain"].Width;
            int HeightofMain = Application.OpenForms["appMain"].Height;
            int LocationMainX = Application.OpenForms["appMain"].Location.X;
            int locationMainy = Application.OpenForms["appMain"].Location.Y;
           
            //Set the Location
            this.Location = new Point(LocationMainX + WidthOfMain, locationMainy);

            AnimateWindow(this.Handle, 250, AnimateWindowFlags.AW_SLIDE | AnimateWindowFlags.AW_HOR_POSITIVE);
            refreshDG();
        }

        private void dgHistory_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
           
            DataGridViewCell xCell = dgHistory.CurrentCell;
            if (xCell != null)
            {
                if (pnd != null)
                {
                    pnd(dgHistory.CurrentRow.Cells["Keyword"].Value.ToString(), dgHistory.CurrentRow.Cells["URL"].Value.ToString());
                }

            }
        }
        private Boolean _hasNewData=false;
        private appMain appMain;
        private void newUpdatedData(Boolean newData)
        {
            _hasNewData = newData;
            if (_hasNewData)
            {
                 try
                    {
                        if (System.IO.File.Exists(XML_PATH))
                        {
                            
                            DataSet ds = new DataSet();
                            ds.ReadXml(XML_PATH);
                            dgHistory.DataSource = ds;
                            dgHistory.DataMember = "History";
                            dgHistory.Refresh();

                        }
                    }
                    catch (Exception)
                    {


                    }
            }
        }

        public void refreshDG()
        {
            try
            {
                if (System.IO.File.Exists(XML_PATH))
                {
                    DataSet ds = new DataSet();
                    ds.ReadXml(XML_PATH);
                    dgHistory.DataSource = ds;
                    dgHistory.DataMember = "History";
                    dgHistory.Refresh();
                }
            }
            catch (Exception)
            {
                
                //throw;
            }

            
        }
        private void bgParentListener_DoWork(object sender, DoWorkEventArgs e)
        {
            appMain ap = new appMain(this);
            ap.update = new NaviRankChecker.appMain.newDataUpdate(newUpdatedData);
            //ap.Show();
            
            while (true)
            {
              
            }
           
        }

        private void cmdCLose_MouseHover(object sender, EventArgs e)
        {
            cmdCLose.Image = Properties.Resources.closeHover;
        }

        private void cmdCLose_MouseLeave(object sender, EventArgs e)
        {
            cmdCLose.Image = Properties.Resources.close;
        }


 
    }
}
